﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WG计划管理工具
{
    public partial class frmWGRKManage : Form
    {
        int iWorkMode = 0;
        SqlTools.SqlTools sqt;
        string strUserID;

        #region SQL脚本
        //获取当天最大的计划单号
        string strGetMaxPPNo = "select max(入库单号) 最大单号 from wgrka where 入库单号 like convert(varchar(100),getdate(),112)+'%'";
        //获取当天最大的结余号
        string strGetMaxJYNo = "select max(结余号) 最大结余号 from wgjy where 结余号 like convert(varchar(100),getdate(),112)+'%'";
        //获取服务器日期
        string strGetServerDate = "select replace(CONVERT(varchar(12) , getdate(), 111),'/','-')";
        //按条件查询指定计划单单身
        string strSelectWGRKARange = "select * from wgrka where 入库单号 in (select distinct b.入库单号 from wgrka a,wgrkb b where a.入库单号=b.入库单号";
        //查询指定计划单单身
        string strSelectWGRKB = "select 入库项次,计划单号,计划项次,型号规格,入库数量,单价,生产批号,备注,结余号 from wgrkb where 入库单号='@入库单号' order by 入库项次";
        //删除指定单号的单头
        string strDeleteWGRKA = "delete from wgrka where 入库单号='@入库单号'";
        //删除指定单号的单身
        string strDeleteWGRKB = "delete from wgrkb where 入库单号='@入库单号'";
        //插入单头
        string strInsertWGRKA = "insert into wgrka (入库单号) values ('@入库单号')";
        //插入单身
        string strInsertWGRKB = "insert into wgrkb (入库单号,入库项次,计划单号,计划项次,型号规格,入库数量,单价,生产批号,备注,结余号) values ('@入库单号',@入库项次,'@计划单号',@计划项次,'@型号规格','@入库数量','@单价','@生产批号','@备注','')";
        //获取单据状态
        string strSelectWGRKMark = "select 单据状态 from wgrka where 入库单号='@入库单号'";
        //更新单据状态
        string strUpdateWGRKMark = "update wgrka set 单据状态=@单据状态 where 入库单号='@入库单号'";
        //更新提交时间
        string strUpdateWGRKSubmitDate = "update wgrka set 提交时间=@提交时间 where 入库单号='@入库单号'";
        //更新审核时间
        string strUpdateWGRKAuditDate = "update wgrka set 审核时间=@审核时间 where 入库单号='@入库单号'";
        //更新WG计划表入库数量
        string strUpdateWGJHRKSL = "update wgjhb set WG入库数量=@WG入库数量 where 计划单号='@计划单号' and 计划项次='@计划项次'";
        //插入结余表记录
        string strInsertWGJY = "insert wgjy (结余号,型号规格,批号,单价,数量,备注) values ('@结余号','@型号规格','@批号',@单价,@数量,'@备注')";
        //更新入库表的结余号
        string strUpdateWGRKJYNo = "update wgrkb set 结余号='@结余号' where 入库单号='@入库单号' and 入库项次='@入库项次'";
        //查询结余是否足够反审核单据
        string strCheckQuantityEnough = "select count(*) from wgrkb,wgjy where wgrkb.结余号=wgjy.结余号 and 数量<>入库数量 and 入库单号='@入库单号' and 入库项次=@入库项次";
        //删除结余记录
        string strDeleteWGJY = "delete from wgjy where 结余号 in (select 结余号 from wgrkb where 入库单号='@入库单号')";
        //检查计划单是否完成
        string strCheckWGJHComplished = "select count(*) from wgjhb where WG计划数量>WG入库数量 and 计划单号='@计划单号'";
        //更新计划单单据状态
        string strUpdateWGJHMark = "update wgjha set 单据状态=@单据状态 where 计划单号='@计划单号'";
        //更新完成时间
        string strUpdateWGJHComplishDate = "update wgjha set 完成时间=@完成时间 where 计划单号='@计划单号'";
        #endregion

        public frmWGRKManage(string strTmpUserID)
        {
            InitializeComponent();
            strUserID = strTmpUserID;
        }

        #region 通用事件
        private void frmWGRKManage_Load(object sender, EventArgs e)
        {
            cmbDocStatus.SelectedIndex = 0;
            ButtonMode(0);
            sqt = new SqlTools.SqlTools(WG计划管理工具.Properties.Settings.Default.ConnectionString);
        }

        private void btnAddKeywords_Click(object sender, EventArgs e)
        {
            if (cmbSearchFields.Text.Length > 0 && txtSearchKeywords.Text.Trim().Length > 0)
            {
                if (lstKeywords.Items.Count > 0)
                {
                    for (int i = 0; i < lstKeywords.Items.Count; i++)
                    {
                        if (lstKeywords.Items[i].ToString().StartsWith(cmbSearchFields.Text))
                        {
                            lstKeywords.Items[i] = cmbSearchFields.Text + ":" + txtSearchKeywords.Text.Trim();
                        }
                    }
                }
                else
                {
                    lstKeywords.Items.Add(cmbSearchFields.Text + ":" + txtSearchKeywords.Text.Trim());
                }
            }
        }

        private void lstKeywords_DoubleClick(object sender, EventArgs e)
        {
            if (lstKeywords.SelectedItem != null)
            {
                lstKeywords.Items.Remove(lstKeywords.SelectedItem);
            }
        }

        private void ButtonMode(int iTmpMode)
        {
            switch (iTmpMode)
            {
                case 0:      //一般工作状态
                    gbSearch.Enabled = true;
                    btnAdd.Enabled = true;
                    btnEdit.Enabled = false ;
                    btnDelete.Enabled = false ;
                    btnConfirm.Enabled = false;
                    btnCancel.Enabled = false;
                    btnGetData.Enabled = false;
                    btnSubmit.Enabled = false;
                    btnAudit.Enabled = false;
                    break;
                case 1:      //添加状态
                    gbSearch.Enabled = false;
                    btnAdd.Enabled = false ;
                    btnEdit.Enabled = false ;
                    btnDelete.Enabled = false ;
                    btnConfirm.Enabled = true ;
                    btnCancel.Enabled = true ;
                    btnGetData.Enabled = true ;
                    btnSubmit.Enabled = false;
                    btnAudit.Enabled = false;
                    break;
                case 2:      //编辑状态
                    gbSearch.Enabled = false;
                    btnAdd.Enabled = false;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;
                    btnConfirm.Enabled = true;
                    btnCancel.Enabled = true;
                    btnGetData.Enabled = true;
                    btnSubmit.Enabled = false;
                    btnAudit.Enabled = false;
                    break;
                case 3:      //查询状态
                    gbSearch.Enabled = true;
                    btnAdd.Enabled = true ;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;
                    btnConfirm.Enabled = false ;
                    btnCancel.Enabled = false ;
                    btnGetData.Enabled = false ;
                    btnSubmit.Enabled = false;
                    btnAudit.Enabled = false;
                    break;
                default :
                    clsTools.ShowWarningMsg(iTmpMode.ToString() + ":未设置的工作状态！");
                    break;
            }
        }

        private void FillRowIndex(DataGridView tmpdgv,string strTmpIDFieldName)
        {
            if (tmpdgv.Rows.Count > 0)
            {
                for (int i = 0; i < tmpdgv.Rows.Count; i++)
                {
                    tmpdgv.Rows[i].Cells[strTmpIDFieldName].Value = i+1;
                }
            }
        }
        #endregion 通用事件

        private string GetPPNo()
        {
            string strTmpMaxPPNo = clsTools.GetString(sqt.DoCmdScalarData(strGetMaxPPNo));
            string strTmpDate = string.Empty;
            string strTmpNo = string.Empty;
            if (strTmpMaxPPNo.Trim().Length == 0)
            {
                strTmpDate = clsTools.GetString(sqt.DoCmdScalarData(strGetServerDate)).Replace("-", "");
                strTmpNo = "0001";
            }
            else
            {
                strTmpDate = strTmpMaxPPNo.Substring(0, 8);
                int iTmpNo = clsTools.GetInt(strTmpMaxPPNo.Substring(8, 4)) + 1;
                strTmpNo = iTmpNo.ToString("0000");
            }
            return strTmpDate + strTmpNo;
        }

        private string GetJYNo()
        {
            string strTmpMaxJYNo = clsTools.GetString(sqt.DoCmdScalarData(strGetMaxJYNo));
            string strTmpDate = string.Empty;
            string strTmpNo = string.Empty;
            if (strTmpMaxJYNo.Trim().Length == 0)
            {
                strTmpDate = clsTools.GetString(sqt.DoCmdScalarData(strGetServerDate)).Replace("-", "");
                strTmpNo = "0001";
            }
            else
            {
                strTmpDate = strTmpMaxJYNo.Substring(0, 8);
                int iTmpNo = clsTools.GetInt(strTmpMaxJYNo.Substring(8, 4)) + 1;
                strTmpNo = iTmpNo.ToString("0000");
            }
            return strTmpDate + strTmpNo;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!clsTools.VerifyPermission(strUserID, "rk3"))
            {
                return;
            }
            iWorkMode = 1;                                                                //设置工作模式为新增模式
            if (dgv1.DataSource != null)
            {
                DataTable tmpdt = (DataTable)dgv1.DataSource;
                tmpdt.Rows.Add(tmpdt.NewRow());
            }
            else
            {
                dgv1.Rows.Add();
            }
            dgv1.Rows[dgv1.Rows.Count - 1].Cells["dgv1_c1"].Value = GetPPNo();
            dgv1.CurrentCell = dgv1.Rows[dgv1.Rows.Count - 1].Cells[0];
            dgv1.Enabled = false;
            lblMark.Text = "";
            lblMark.Visible = false;
            dgv2.ContextMenuStrip = contextMenuStrip1;
            dgv2.Rows.Clear();
            dgv2.ReadOnly = false;
            ButtonMode(1);
        }

        private void btnGetData_Click(object sender, EventArgs e)
        {
            frmJHSelector newFrmJHSelector = new frmJHSelector();
            newFrmJHSelector.Owner = this.Owner;
            newFrmJHSelector.ShowDialog();
            if (newFrmJHSelector.bCancel || newFrmJHSelector.dfrlst == null)
            {
                return;
            }
            if (newFrmJHSelector.dfrlst.Count > 0)
            {
                dgv2.Rows.Add(newFrmJHSelector.dfrlst.Count);
                for (int i = 0; i < newFrmJHSelector.dfrlst.Count; i++)
                {
                    dgv2.Rows[dgv2.Rows.Count - newFrmJHSelector.dfrlst.Count + i].Cells["dgv2_c2"].Value = newFrmJHSelector.dfrlst[i].strPPNo;
                    dgv2.Rows[dgv2.Rows.Count - newFrmJHSelector.dfrlst.Count + i].Cells["dgv2_c3"].Value = newFrmJHSelector.dfrlst[i].strPX;
                    dgv2.Rows[dgv2.Rows.Count - newFrmJHSelector.dfrlst.Count + i].Cells["dgv2_c4"].Value = newFrmJHSelector.dfrlst[i].strModel;
                    dgv2.Rows[dgv2.Rows.Count - newFrmJHSelector.dfrlst.Count + i].Cells["dgv2_c5"].Value = newFrmJHSelector.dfrlst[i].iQuantity;
                }
                dgv2.AutoResizeColumns();
                FillRowIndex(dgv2,"dgv2_c1");
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (!clsTools.VerifyPermission(strUserID, "rk2"))
            {
                return;
            }
            string strTmpSelectString = strSelectWGRKARange;
            for (int i = 0; i < lstKeywords.Items.Count; i++)
            {
                string strTmpFieldName = lstKeywords.Items[i].ToString().Split(':').GetValue(0).ToString();
                string strTmpKeyword = lstKeywords.Items[i].ToString().Split(':').GetValue(1).ToString();
                switch (strTmpFieldName)
                {
                    case "出库单号":
                        strTmpSelectString += " and b.出库单号 like '" + strTmpKeyword + "'";
                        break;
                    case "计划单号":
                        strTmpSelectString += " and 计划单号 like '" + strTmpKeyword + "'";
                        break;
                    case "型号规格":
                        strTmpSelectString += " and 型号规格 like '" + strTmpKeyword + "'";
                        break;
                    case "结余号":
                        strTmpSelectString += " and 结余号 like '" + strTmpKeyword + "'";
                        break;
                }
            }
            if (cmbDocStatus.SelectedIndex != 0)
            {
                strTmpSelectString += " and 单据状态=" + Convert.ToString(cmbDocStatus.SelectedIndex-1);
            }
            strTmpSelectString += ")";
            dgv1 .DataSource = sqt.GetDataTable(strTmpSelectString);
            if (dgv1.Rows.Count > 0)
            {
                dgv1.CurrentCell = dgv1.Rows[0].Cells[0];
            }
        }

        private void ReadData()
        {
            ButtonMode(3);
            if (dgv1.CurrentRow != null)
            {
                string strTmpRkNo = clsTools.GetString(dgv1.CurrentRow.Cells["dgv1_c1"].Value);
                DataTable tmpdt = sqt.GetDataTable(strSelectWGRKB.Replace("@入库单号", strTmpRkNo));
                switch (clsTools.GetString(dgv1.CurrentRow.Cells["dgv1_c4"].Value).Trim ())
                {
                    case "0":
                        lblMark.Text = "";
                        lblMark.Visible = false;
                        btnDelete.Enabled = true;
                        btnSubmit.Enabled = true;
                        btnAudit.Enabled = false;
                        btnEdit.Enabled = true;
                        dgv2.Columns["dgv2_c5"].ReadOnly = true;
                        dgv2.Columns["dgv2_c6"].ReadOnly = true;
                        dgv2.Columns["dgv2_c7"].ReadOnly = true;
                        dgv2.Columns["dgv2_c8"].ReadOnly = true;
                        break;
                    case "1":
                        lblMark.Text = "提";
                        lblMark.Visible = true ;
                        btnDelete.Enabled = false;
                        btnSubmit.Enabled = true;
                        btnAudit.Enabled = true;
                        btnEdit.Enabled = true;
                        dgv2.Columns["dgv2_c5"].ReadOnly = false;
                        dgv2.Columns["dgv2_c6"].ReadOnly = false;
                        dgv2.Columns["dgv2_c7"].ReadOnly = false;
                        dgv2.Columns["dgv2_c8"].ReadOnly = false;
                        break;
                    case "2":
                        lblMark.Text = "核";
                        lblMark.Visible = true;
                        btnDelete.Enabled = false;
                        btnSubmit.Enabled = false;
                        btnAudit.Enabled = true;
                        btnEdit.Enabled = false ;
                        dgv2.Columns["dgv2_c5"].ReadOnly = true;
                        dgv2.Columns["dgv2_c6"].ReadOnly = true;
                        dgv2.Columns["dgv2_c7"].ReadOnly = true;
                        dgv2.Columns["dgv2_c8"].ReadOnly = true;
                        break;
                }
                dgv2.Rows.Clear();
                if (tmpdt.Rows.Count > 0)
                {
                    dgv2.Rows.Add(tmpdt.Rows.Count);
                    for (int i = 0; i < tmpdt.Rows.Count; i++)
                    {
                        dgv2.Rows[i].Cells["dgv2_c1"].Value = tmpdt.Rows[i]["入库项次"];
                        dgv2.Rows[i].Cells["dgv2_c2"].Value = tmpdt.Rows[i]["计划单号"];
                        dgv2.Rows[i].Cells["dgv2_c3"].Value = tmpdt.Rows[i]["计划项次"];
                        dgv2.Rows[i].Cells["dgv2_c4"].Value = tmpdt.Rows[i]["型号规格"];
                        dgv2.Rows[i].Cells["dgv2_c5"].Value = tmpdt.Rows[i]["入库数量"];
                        dgv2.Rows[i].Cells["dgv2_c6"].Value = tmpdt.Rows[i]["单价"];
                        dgv2.Rows[i].Cells["dgv2_c7"].Value = tmpdt.Rows[i]["生产批号"];
                        dgv2.Rows[i].Cells["dgv2_c8"].Value = tmpdt.Rows[i]["备注"];
                        dgv2.Rows[i].Cells["dgv2_c9"].Value = tmpdt.Rows[i]["结余号"];
                    }
                    dgv2.AutoResizeColumns();
                }
            }
        }

        private void dgv1_CurrentCellChanged(object sender, EventArgs e)
        {
            ReadData();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (!clsTools.VerifyPermission(strUserID, "rk4"))
            {
                return;
            }
            iWorkMode = 2;                                                                //设置工作模式为编辑模式
            dgv1.Enabled = false;
            dgv2.ContextMenuStrip = contextMenuStrip1;
            dgv2.Rows.Clear();
            dgv2.ReadOnly = false;
            ButtonMode(2);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (!clsTools.VerifyPermission(strUserID, "rk5"))
            {
                return;
            }
            if (dgv1.CurrentRow != null)
            {
                string strTmpRkNo = clsTools.GetString(dgv1.CurrentRow.Cells["dgv1_c1"].Value).Trim();
                DialogResult dr = MessageBox.Show("确定删除单号为" + strTmpRkNo + "的入库单？删除以后将无法恢复！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (dr == DialogResult.OK)
                {
                    sqt.DoCmdExecuteNonQuery(strDeleteWGRKA.Replace("@入库单号", strTmpRkNo));
                    sqt.DoCmdExecuteNonQuery(strDeleteWGRKB.Replace("@入库单号", strTmpRkNo));
                    dgv1.Rows.Remove(dgv1.CurrentRow);
                }
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            switch (iWorkMode)
            {
                case 1:
                    if (dgv2.Rows.Count == 0)
                    {
                        clsTools.ShowWarningMsg("单身没有内容，无法保存该单据！");
                        return;
                    }
                    else
                    {
                        for (int i = dgv2.Rows.Count - 1; i >= 0; i--)
                        {
                            bool bBlankRow = true;
                            foreach (DataGridViewCell tmpdgvc in dgv2.Rows[i].Cells)
                            {
                                if (tmpdgvc.ColumnIndex == 0)
                                {
                                    continue;
                                }
                                if (clsTools.GetString(tmpdgvc.Value).Trim().Length > 0)
                                {
                                    bBlankRow = false;
                                    break;
                                }
                            }
                            if (!bBlankRow)
                            {
                                break;
                            }
                            else
                            {
                                dgv2.Rows.RemoveAt(i);
                            }
                        }
                        FillRowIndex(dgv2,"dgv2_c1");
                        if (dgv2.Rows.Count == 0)
                        {
                            clsTools.ShowWarningMsg("单身没有内容，无法保存该单据！");
                            return;
                        }
                        else
                        {
                            string strTmpRkNo = GetPPNo();
                            dgv1.CurrentRow .Cells ["dgv1_c1"].Value = strTmpRkNo ;
                            if (sqt.DoCmdExecuteNonQuery(strInsertWGRKA.Replace("@入库单号", strTmpRkNo)) > 0)
                            {
                                for (int i = 0; i < dgv2.Rows.Count; i++)
                                {
                                    sqt.DoCmdExecuteNonQuery(strInsertWGRKB.Replace("@入库单号", strTmpRkNo)
                                                                                                                         .Replace("@入库项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value))
                                                                                                                         .Replace("@计划单号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value))
                                                                                                                         .Replace("@计划项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c3"].Value))
                                                                                                                        .Replace("@型号规格", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c4"].Value))
                                                                                                                        .Replace("@入库数量", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c5"].Value))
                                                                                                                        .Replace("@单价", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c6"].Value))
                                                                                                                        .Replace("@生产批号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c7"].Value))
                                                                                                                        .Replace("@备注", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c8"].Value)));
                                }
                            }
                            ButtonMode(0);
                            btnSubmit.Enabled = true;
                            btnConfirm.Enabled = false ;
                            dgv2.ContextMenuStrip = null;
                            dgv1.Enabled =true ;
                            iWorkMode = 0;
                        }
                    }
                    break;
                case 2:
                    if (dgv2.Rows.Count == 0)
                    {
                        clsTools.ShowWarningMsg("单身没有内容，无法保存该单据！");
                        return;
                    }
                    else
                    {
                        for (int i = dgv2.Rows.Count - 1; i >= 0; i--)
                        {
                            bool bBlankRow = true;
                            foreach (DataGridViewCell tmpdgvc in dgv2.Rows[i].Cells)
                            {
                                if (tmpdgvc.ColumnIndex == 0)
                                {
                                    continue;
                                }
                                if (clsTools.GetString(tmpdgvc.Value).Trim().Length > 0)
                                {
                                    bBlankRow = false;
                                    break;
                                }
                            }
                            if (!bBlankRow)
                            {
                                break;
                            }
                            else
                            {
                                dgv2.Rows.RemoveAt(i);
                            }
                        }
                        FillRowIndex(dgv2, "dgv2_c1");
                        if (dgv2.Rows.Count == 0)
                        {
                            clsTools.ShowWarningMsg("单身没有内容，无法保存该单据！");
                            return;
                        }
                        else
                        {
                            string strTmpRkNo = clsTools.GetString(dgv1.CurrentRow.Cells["dgv1_c1"].Value).Trim();
                            sqt.DoCmdExecuteNonQuery(strDeleteWGRKB.Replace("@入库单号", strTmpRkNo));
                            for (int i = 0; i < dgv2.Rows.Count; i++)
                            {
                                sqt.DoCmdExecuteNonQuery(strInsertWGRKB.Replace("@入库单号", strTmpRkNo)
                                                                                                                         .Replace("@入库项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value))
                                                                                                                         .Replace("@计划单号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value))
                                                                                                                         .Replace("@计划项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c3"].Value))
                                                                                                                        .Replace("@型号规格", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c4"].Value))
                                                                                                                        .Replace("@入库数量", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c5"].Value))
                                                                                                                        .Replace("@单价", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c6"].Value))
                                                                                                                        .Replace("@生产批号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c7"].Value))
                                                                                                                        .Replace("@备注", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c8"].Value)));
                                ButtonMode(0);
                                btnSubmit.Enabled = true;
                                btnConfirm.Enabled = false ;
                                dgv2.ContextMenuStrip = null;
                                dgv1.Enabled = true;
                                iWorkMode = 0;
                            }
                        }
                    }
                    break;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            switch (iWorkMode)
            {
                case 1:
                    iWorkMode = 0;
                    ButtonMode(0);
                    dgv1.Rows.Remove(dgv1.CurrentRow);
                    dgv2.Rows.Clear();
                    dgv1.Enabled = true;
                    dgv2.ReadOnly = true;
                    dgv2.ContextMenuStrip = null;
                    ReadData();
                    break;
                case 2:
                    iWorkMode = 0;
                    ButtonMode(0);
                    dgv1.Enabled = true;
                    dgv2.ReadOnly = true;
                    dgv2.ContextMenuStrip = null;
                    ReadData();
                    break;
            }
        }

        private void 删除行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgv2.CurrentRow != null)
            {
                dgv2.Rows.Remove(dgv2.CurrentRow);
            }
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (!clsTools.VerifyPermission(strUserID, "rk6"))
            {
                return;
            }
            if (dgv1.CurrentRow != null)
            {
                string strTmpRkNo = clsTools.GetString(dgv1.CurrentRow.Cells["dgv1_c1"].Value);
                int iMark = clsTools.GetInt(sqt.DoCmdScalarData(strSelectWGRKMark.Replace("@入库单号", strTmpRkNo)));
                if (iMark == 0)
                {
                    sqt.DoCmdExecuteNonQuery(strUpdateWGRKMark.Replace("@入库单号", strTmpRkNo)
                                                                                                                    .Replace("@单据状态", "1"));
                    sqt.DoCmdExecuteNonQuery(strUpdateWGRKSubmitDate.Replace("@入库单号", strTmpRkNo)
                                                                                                                                    .Replace("@提交时间", "replace(CONVERT(varchar(12) , getdate(), 111),'/','-')"));
                    dgv1.CurrentRow.Cells["dgv1_c2"].Value = clsTools.GetString(sqt.DoCmdScalarData(strGetServerDate));
                    lblMark.Text = "提";
                    lblMark.Visible = true;
                    btnEdit.Enabled = false;
                    btnDelete.Enabled = false;
                    btnCancel.Enabled = false;
                    btnConfirm.Enabled = false;
                    btnAudit.Enabled = true;
                }
                else
                {
                    if (iMark == 1)
                    {
                        sqt.DoCmdExecuteNonQuery(strUpdateWGRKMark.Replace("@入库单号", strTmpRkNo)
                                                                                                                    .Replace("@单据状态", "0"));
                        sqt.DoCmdExecuteNonQuery(strUpdateWGRKSubmitDate.Replace("@入库单号", strTmpRkNo)
                                                                                                                                    .Replace("@提交时间", "null"));
                        dgv1.CurrentRow.Cells["dgv1_c2"].Value = string.Empty;
                        lblMark.Text = "";
                        lblMark.Visible = false;
                        btnEdit.Enabled = true;
                        btnDelete.Enabled = true;
                        btnCancel.Enabled = false;
                        btnConfirm.Enabled = false;
                        btnAudit.Enabled = false;
                    }
                }
            }
        }

        private void btnAudit_Click(object sender, EventArgs e)
        {
            if (!clsTools.VerifyPermission(strUserID, "rk7"))
            {
                return;
            }
            if (dgv1.CurrentRow != null)
            {
                string strTmpRkNo = clsTools.GetString(dgv1.CurrentRow.Cells["dgv1_c1"].Value);
                int iMark = clsTools.GetInt(sqt.DoCmdScalarData(strSelectWGRKMark.Replace("@入库单号", strTmpRkNo)));
                if (iMark == 1)
                {
                    for (int i = 0; i < dgv2.Rows.Count; i++)
                    {
                        string strTmpJyNo = GetJYNo();
                        //插入新结余
                        sqt.DoCmdExecuteNonQuery(strInsertWGJY.Replace("@结余号", strTmpJyNo)
                                                                                            .Replace("@型号规格", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c4"].Value))
                                                                                            .Replace("@批号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c7"].Value))
                                                                                            .Replace("@单价", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c6"].Value))
                                                                                            .Replace("@数量", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c5"].Value))
                                                                                            .Replace("@备注", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c8"].Value)));
                        //更新计划表入库数量
                        sqt.DoCmdExecuteNonQuery(strUpdateWGJHRKSL.Replace("@计划单号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value))
                                                                                                    .Replace("@计划项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c3"].Value))
                                                                                                    .Replace("@WG入库数量", "WG入库数量+" + clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c5"].Value)));
                        //更新入库单结余号
                        sqt .DoCmdExecuteNonQuery (strUpdateWGRKJYNo .Replace ("@入库单号",strTmpRkNo )
                                                                                                        .Replace ("@入库项次",clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value))
                                                                                                        .Replace ("@结余号",strTmpJyNo ));
                        dgv2.Rows[i].Cells["dgv2_c9"].Value = strTmpJyNo;
                        //检查计划表是否完成
                        if (clsTools.GetInt(sqt.DoCmdScalarData(strCheckWGJHComplished.Replace("@计划单号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value)))) == 0)
                        {
                            sqt.DoCmdExecuteNonQuery(strUpdateWGJHMark.Replace("@计划单号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value))
                                                                                                            .Replace("@单据状态", "3"));
                            sqt.DoCmdExecuteNonQuery(strUpdateWGJHComplishDate.Replace("@计划单号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value))
                                                                                                                        .Replace("@完成时间", "replace(CONVERT(varchar(12) , getdate(), 111),'/','-')"));
                        }
                    }
                    sqt.DoCmdExecuteNonQuery(strUpdateWGRKMark.Replace("@入库单号", strTmpRkNo)
                                                                                                  .Replace("@单据状态", "2"));
                    sqt.DoCmdExecuteNonQuery(strUpdateWGRKAuditDate.Replace("@入库单号", strTmpRkNo)
                                                                                                          .Replace("@审核时间", "replace(CONVERT(varchar(12) , getdate(), 111),'/','-')"));
                    dgv1.CurrentRow.Cells["dgv1_c3"].Value = clsTools.GetString(sqt.DoCmdScalarData(strGetServerDate));
                    lblMark.Text = "核";
                    btnSubmit.Enabled = false;
                }
                else
                {
                    if (iMark == 2)
                    {
                        //确定是否有足够结余反审核单据
                        for (int i = 0; i < dgv2.Rows.Count; i++)
                        {
                            if (clsTools.GetInt(sqt.DoCmdScalarData(strCheckQuantityEnough.Replace("@入库单号", strTmpRkNo)
                                                                                                                                .Replace("@入库项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value)))) > 0)
                            {
                                clsTools.ShowWarningMsg("第" + Convert.ToString(i + 1) + "项结余数量不等于入库数量，无法反审核单据！");
                                return;
                            }
                        }
                        sqt.DoCmdExecuteNonQuery(strDeleteWGJY.Replace("@入库单号", strTmpRkNo));
                        for (int i = 0; i < dgv2.Rows.Count; i++)
                        {
                            //更新计划表入库数量
                            sqt.DoCmdExecuteNonQuery(strUpdateWGJHRKSL.Replace("@计划单号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value))
                                                                                                        .Replace("@计划项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c3"].Value))
                                                                                                        .Replace("@WG入库数量", "WG入库数量-" + clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c5"].Value)));
                            //更新入库单结余号
                            sqt.DoCmdExecuteNonQuery(strUpdateWGRKJYNo.Replace("@入库单号", strTmpRkNo)
                                                                                                            .Replace("@入库项次", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c1"].Value))
                                                                                                            .Replace("@结余号", string.Empty ));
                            dgv2.Rows[i].Cells["dgv2_c9"].Value = string.Empty ;
                            sqt.DoCmdExecuteNonQuery(strUpdateWGJHMark.Replace("@计划单号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value))
                                                                                                            .Replace("@单据状态", "2"));
                            sqt.DoCmdExecuteNonQuery(strUpdateWGJHComplishDate.Replace("@计划单号", clsTools.GetString(dgv2.Rows[i].Cells["dgv2_c2"].Value))
                                                                                                                        .Replace("@完成时间", string.Empty ));
                        }
                        sqt.DoCmdExecuteNonQuery(strUpdateWGRKMark.Replace("@入库单号", strTmpRkNo)
                                                                                                      .Replace("@单据状态", "1"));
                        sqt.DoCmdExecuteNonQuery(strUpdateWGRKAuditDate.Replace("@入库单号", strTmpRkNo)
                                                                                                              .Replace("@审核时间", "null"));
                        dgv1.CurrentRow.Cells["dgv1_c3"].Value = string.Empty;
                        lblMark.Text = "提";
                        btnSubmit.Enabled = true;
                    }
                }
            }
        }
    }
}